package com.china08.yxyapi.api.manage.edu.platform.continuedu.lesson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.service.continuedu.YxyContinueduLessonService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduLessonListController extends YxyApiBaseController {

    @Autowired
    YxyContinueduLessonService lessonService;

    @Autowired
    YxyContinueduLessonItemRepository itemRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/lesson/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        // 1：上架，2：下架，3：草稿
        Integer lessonType = reqModel.lessonType;
        String keyword = reqModel.keyword;
        keyword = StringTrimUtil.trim(keyword);
        keyword = (StringUtils.isNotBlank(keyword)) ? "%" + keyword + "%" : "";
        Integer pageNum = reqModel.pageNum;
        Map<String, Object> returnMap = lessonService.findList(schoolId, lessonType, keyword, pageNum);

        Integer allPage = 0;
        List<Pojo> returnList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        if (returnMap != null) {
            allPage = Integer.parseInt(returnMap.get("allSize") + "");
            list = (List<Map<String, Object>>) returnMap.get("list");
        }

        for (Map<String, Object> map : list) {
            Pojo pojo = new Pojo();
            String lessonId = map.get("id") + "";
            String lessonName = map.get("name") + "";
            String lessonCover = map.get("cover") + "";
            String author = map.get("author") + "";
            pojo.isVisible = Integer.parseInt(map.get("is_visible") + "");
            pojo.lessonId = lessonId;
            pojo.lessonName = lessonName;
            pojo.lessonCover = lessonCover;
            pojo.author = author;
            // 创建时间
            Object obj1 = map.get("created_date");
            if (obj1 != null) {
                pojo.createDate = DateToolUtils.format((Date) obj1, "yyyy-MM-dd");
            }
            // 最后修改时间
            Object obj2 = map.get("last_modified_date");
            if (obj2 != null) {
                pojo.lastModifiedDate = DateToolUtils.format((Date) obj2, "yyyy-MM-dd");
            }
            // 上架时间
            Object obj3 = map.get("up_date");
            if (obj3 != null) {
                pojo.upDate = DateToolUtils.format((Date) obj3, "yyyy-MM-dd");
            }
            // 下架时间
            Object obj4 = map.get("down_date");
            if (obj4 != null) {
                pojo.downDate = DateToolUtils.format((Date) obj4, "yyyy-MM-dd");
            }

            // 获取课程下面的总课时
            Integer itemCount = itemRepository.countByLessonId(lessonId);
            pojo.itemCount = itemCount;

            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.list = returnList;
        return respModel;

    }

    public static class Pojo {
        public String lessonId;
        public String lessonName;
        public String lessonCover;
        public String author;
        public String createDate;
        public String lastModifiedDate;
        public Integer itemCount;
        public Integer isVisible;
        public String upDate;
        public String downDate;
    }

    public static class ReqModel extends YxyApiReqModel {
        public Integer lessonType;
        public String keyword;
        public Integer pageNum;

        @Override
        public void autoCheck() {
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allPage;
        public List<Pojo> list;
    }
}
